package com.example.foobar.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.foobar.business.service.DaDataAnalysisService;
import com.example.foobar.business.service.DaNewCommerCountService;
import com.example.foobar.dao.service.DaDataAnalysisDaoService;
import com.example.foobar.dao.service.ImsPzGuanhuaiTrackDaoService;
import com.example.foobar.dto.DABaseDTO;
import com.example.foobar.entity.DaDataAnalysis;
import com.example.foobar.vo.AnalysisData;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 新增用户数记录 服务实现类
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@Service
public class DaDataAnalysisServiceImpl implements DaDataAnalysisService {

    public static final int WEEK_DAYS_COUNT = 7;

    @Autowired
    private DaNewCommerCountService daNewCommerCountService;

    @Autowired
    private DaDataAnalysisDaoService dataAnalysisDaoService;

    @Autowired
    private ImsPzGuanhuaiTrackDaoService imsPzGuanhuaiTrackDaoService;

    public boolean saveDaDataAnalysis(DaDataAnalysis daDataAnalysis) throws Exception {
        return dataAnalysisDaoService.save(daDataAnalysis);
    }

    public boolean updateDaDataAnalysis(DaDataAnalysis daDataAnalysis) throws Exception {
        return dataAnalysisDaoService.updateById(daDataAnalysis);
    }

    public boolean deleteDaDataAnalysis(Long id) throws Exception {
        return dataAnalysisDaoService.removeById(id);
    }

    @Override
    public DaDataAnalysis getById(Long id) {
        return dataAnalysisDaoService.getById(id);
    }

    @Override
    public Paging<AnalysisData> getDaDataAnalysisPageList(String dateStr, String uniacid) throws Exception {
        List<AnalysisData> result = new ArrayList<>();
        Date date = DateUtils.parseDate(dateStr, "YYYY-MM-dd");
        for(int i = 0; i < 7; i++){
            int currentDayNewCommerCount = daNewCommerCountService.selectNewCommerCount(uniacid, date);
            List<DABaseDTO> baseDateList = getBaseDateList(uniacid, date);
            AnalysisData analysisData = buildBaseDateDto(baseDateList, currentDayNewCommerCount,date);
            date = DateUtils.addDays(date, 1);
            result.add(analysisData);
        }
        Collections.sort(result, (o1, o2) -> o2.getDate().compareTo(o1.getDate()));
        IPage<AnalysisData> page = new Page<>();
        page.setPages(1);
        page.setRecords(result);
        page.setCurrent(1);
        page.setSize(1);
        page.setTotal(7);
        Paging<AnalysisData> paging = new Paging<AnalysisData>(page);
        return paging;
    }

    private AnalysisData buildBaseDateDto(List<DABaseDTO> baseDateList, int currentDayNewCommerCount, Date date) {
        AnalysisData analysisData = new AnalysisData();
        List<DABaseDTO> sortedList = baseDateList.stream().sorted(Comparator.comparing(DABaseDTO::getDate)).collect(Collectors.toList());
        analysisData.setFirstDayRate(formateRate(0, sortedList, currentDayNewCommerCount));
        analysisData.setSecondDayRate(formateRate(1, sortedList, currentDayNewCommerCount));
        analysisData.setThirdDayRate(formateRate(2, sortedList, currentDayNewCommerCount));
        analysisData.setFourthDayRate(formateRate(3, sortedList, currentDayNewCommerCount));
        analysisData.setFifthDayRate(formateRate(4, sortedList, currentDayNewCommerCount));
        analysisData.setSixthDayRate(formateRate(5, sortedList, currentDayNewCommerCount));
        analysisData.setSeventhDayRate(formateRate(6, sortedList, currentDayNewCommerCount));
        analysisData.setEighthDayRate(formateRate(7, sortedList, currentDayNewCommerCount));
        analysisData.setNinthDayRate(formateRate(8, sortedList, currentDayNewCommerCount));
        analysisData.setTenthDayRate(formateRate(9, sortedList, currentDayNewCommerCount));
        analysisData.setEleventhDayRate(formateRate(10, sortedList, currentDayNewCommerCount));
        analysisData.setTwelfthDayRate(formateRate(11, sortedList, currentDayNewCommerCount));
        analysisData.setThirteenthDayRate(formateRate(12, sortedList, currentDayNewCommerCount));
        analysisData.setFourteenthDayRate(formateRate(13, sortedList, currentDayNewCommerCount));
        analysisData.setFifteenthDayRate(formateRate(14, sortedList, currentDayNewCommerCount));
        analysisData.setDate(date);
        analysisData.setNewCommerCount(currentDayNewCommerCount);
        return analysisData;
    }

    private String formateRate(int index, List<DABaseDTO> list, int currentDayNewCommerCount){
        if(index >= list.size()){
            return "/";
        }
        double rate = (double)list.get(index).getCount()/currentDayNewCommerCount;
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);
        return nt.format(rate);
    }

    private List<DABaseDTO> getBaseDateList(String uniacid, Date data) {
        Map<String, Object> params = new HashMap<>();
        params.put("uniacid" , uniacid);
        params.put("baseDate" , data);
        params.put("startDate" , data);
        params.put("endDate" , DateUtils.addDays(data, 14));
        params.put("messageTypes" , "'" + DaNewCommerCountServiceImpl.MESSAGE_TYPE_EVENT + "','"  + DaNewCommerCountServiceImpl.MESSAGE_TYPE_TEXT +"'");
        List<DABaseDTO> list = imsPzGuanhuaiTrackDaoService.getActiveDataByOfficialAccountIdAndDataRange(params);
        return list;
    }


}
